Ext.define('Admin.view.news.NewsController', {

    extend: 'Admin.ux.GridController',

    alias: 'controller.news',
    //编辑行
    moreAction: function (grid, rowIndex, colIndex, item, e, record, row) {

        var rec = grid.getStore().getAt(rowIndex);

        var me = this;

        var treeMenu = new Ext.menu.Menu({
            shadow: "drop",
            allowOtherMenus: true,
            items: [
                new Ext.menu.Item({
                    text: "编辑",
                    iconCls: 'array-grid-buy-col',
                    handler:function(grid, rowIndex, colIndex,item,e,record,row){



                        var win = Ext.create("Admin.ux.Window",{
                            title:'编辑信息',
                            width:710,
                            height:550,
                            animateTarget: row,
                            items:{xtype:'news-form'},
                            buttons:[
                                {
                                    text:'保存',
                                    ui: 'soft-green',
                                    iconCls: 'x-fa 	fa-floppy-o',
                                    handler:function(btn){
                                        var form = btn.up("window").down("form").getForm();
                                        Ext.create('Admin.ux.Action').submit(form,'/news/edit',me.getView(),win);
                                    }
                                },
                                {
                                    text:'重置',
                                    ui: 'soft-red',
                                    iconCls: 'x-fa 	fa-mail-reply',
                                    handler:function(btn){
                                        var form = btn.up("window").down("form");
                                        form.getForm().load({
                                            url: '/news/get',
                                            waitMsg:'加载中...',
                                            params: {
                                                id: rec.get('id')
                                            },
                                            success:function(){
                                                form.down('textfield[id=id]').setReadOnly(true);
                                            },
                                            failure: function(form, action) {
                                                if(action.result.msg!=undefined){
                                                    Ext.Msg.alert("提示", "<font color=red>"+action.result.msg+"!</font>");
                                                }else{
                                                    Ext.Msg.alert("提示", "<font color=red>加载出错,请刷新页面重试!</font>");
                                                }
                                            }
                                        });
                                        form.loadRecord(rec);
                                    }
                                },
                                {
                                    text:'取消',
                                    ui: 'gray',
                                    iconCls: 'x-fa 	fa-ban',
                                    handler:function(btn){
                                        btn.up("window").close();
                                    }
                                }
                            ],

                            listeners:{
                                show:function(w,opts){
                                    var form = this.down('form');
                                    form.getForm().load({
                                        url: '/news/get',
                                        waitMsg:'加载中...',
                                        params: {
                                            id: rec.get('id')
                                        },
                                        success:function(){
                                            form.down('textfield[id=id]').setReadOnly(true);
                                        },
                                        failure: function(form, action) {
                                            if(action.result.msg!=undefined){
                                                Ext.Msg.alert("提示", "<font color=red>"+action.result.msg+"!</font>");
                                            }else{
                                                Ext.Msg.alert("提示", "<font color=red>加载出错,请刷新页面重试!</font>");
                                            }
                                        }
                                    });
                                }
                            }
                        });

                    }
                }),
                new Ext.menu.Separator(),
                new Ext.menu.Item({
                    text: "浏览",
                    iconCls: 'array-grid-buy-col',
                    handler:function(){
                        var _id = rec.data.id;
                        window.open("http://140.143.121.69/TenderWeb/ControlSite/ViewArticle?id=" + _id);
                    }
                }),
                new Ext.menu.Separator(),
                new Ext.menu.Item({
                    text: "删除",
                    iconCls: 'array-grid-buy-col',
                    handler: function(grid, rowIndex, colIndex,item,e,record,row){
                        var _id = rec.data.id;
                        Ext.create('Admin.ux.Action').remove({id:_id},'/news/delete',me.getView(),row);
                    }
                })
            ]
        });
        treeMenu.showAt(e.getPoint());
    },

    reviewAction: function (grid, rowIndex, colIndex, item, e, record, row) {

        var rec = grid.getStore().getAt(rowIndex);

        var me = this;
        var _id = rec.data.id;
        Ext.create('Admin.ux.Action').check({id:_id,IsChecked:1},'/news/check',me.getView(),row);
    },
    detailAction: function (grid, rowIndex, colIndex, item, e, record, row) {

        var rec = grid.getStore().getAt(rowIndex);

        var me = this;
        var _id = rec.data.id;
        window.open("http://140.143.121.69/TenderWeb/ControlSite/ViewArticle?id=" + _id);
    },
    //新增
    addButton: function(b,opts) {

        var me = this;
        var form = b.up("form").getForm();
        var viewModel = me.getViewModel();
        form.submit({
            clientValidation: true,
            url:'/news/add',
            params:form.getFieldValues(),
            success:function(form,action){
                me.redirectTo('news-view', true);
            },
            failure:function(form,action){
                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.create('Admin.ux.Action').error('客户端验证不通过');
                        break;
                    default:
                        viewModel.set("error",action.result.msg+"!");
                }
            }
        });
    },
});